<template>
  <el-card shadow="hover" class="mt-20">
    <el-button
      type="primary"
      :icon="CirclePlusFilled"
      @click="showAddSpu"
      :disabled="!categoryStore.category3Id"
      >添加SPU</el-button
    >
    <el-table :data="spuList" border class="mt-20" v-loading="loading">
      <el-table-column label="序号" type="index" width="60" align="center" />
      <el-table-column label="SPU名称" prop="spuName" />
      <el-table-column label="SPU描述" prop="description" />
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button
            type="primary"
            size="small"
            :icon="Plus"
            title="添加SKU"
            @click="showAddSku(row)" />
          <el-button
            type="warning"
            size="small"
            :icon="Edit"
            title="修改SPU"
            @click="showUpdateSpu(row)" />
          <el-button
            type="info"
            size="small"
            :icon="InfoFilled"
            title="查看SKU列表"
            @click="showSkuList(row.id, row.spuName)" />
          <el-popconfirm
            @confirm="delSpuItem(row)"
            :title="`确认要删除${row.spuName}吗?`">
            <template #reference>
              <el-button
                type="danger"
                size="small"
                :icon="Delete"
                title="删除SPU" />
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="mt-20"
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 6, 9, 11]"
      layout="prev, pager, next, jumper,->,sizes, total"
      :total="total"
      @size-change="getSpuList"
      @current-change="getSpuList" />

    <el-dialog v-model="dialogVisible" title="xxx的SKU列表">
      <el-table :data="skuList" v-loading="skuLoading">
        <el-table-column prop="skuName" label="名称" />
        <el-table-column prop="price" label="价格(元)" />
        <el-table-column prop="weight" label="重量(KG)" />
        <el-table-column prop="address" label="默认图片">
          <template v-slot="{ row }">
            <img
              :src="row.skuDefaultImg"
              :alt="row.skuName"
              style="width: 100px" />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "SupList",
};
</script>

<script lang="ts" setup>
import { ref, watch } from "vue";
import { getSpuListApi, deleteSpuApi } from "@/api/product/spu";
import { getSkuListBySpuIdApi } from "@/api/product/sku";
import { SkuList } from "@/api/product/model/skuModel";
import {
  CirclePlusFilled,
  Delete,
  Plus,
  Edit,
  InfoFilled,
} from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import type { SpuList, SpuItem } from "@/api/product/model/spuModel";
import { useCategoryStore } from "@/stores/category";
import { useSpuStore } from "@/stores/spu";
const categoryStore = useCategoryStore();
const spuStore = useSpuStore();

// 查看Sku
const dialogVisible = ref(false);
const skuLoading = ref(false);
const skuList = ref<SkuList>([]);
const spuName = ref("");

const showSkuList = async (spuId: number, name: string) => {
  skuLoading.value = true;
  dialogVisible.value = true;
  spuName.value = name;
  skuList.value = await getSkuListBySpuIdApi(spuId);
  skuLoading.value = false;
};

// load加载效果
const loading = ref(false);
// 自定义函数
const emit = defineEmits(["setIsShow"]);

const currentPage = ref(1);
const pageSize = ref(3);
const total = ref(0);
const spuList = ref<SpuList>([]);

// 封装发送获取数据列表展示
const getSpuList = async () => {
  loading.value = true;
  const res = await getSpuListApi({
    page: currentPage.value,
    limit: pageSize.value,
    category3Id: categoryStore.category3Id as number,
  });
  spuList.value = res.records;
  total.value = res.total;
  loading.value = false;
};

// 添加Spu按钮
const showAddSpu = () => {
  emit("setIsShow", 2);
  spuStore.$reset();
};
// 添加Sku按钮
const showAddSku = (row: SpuItem) => {
  emit("setIsShow", 3);
  spuStore.spu = row;
};

// 修改Spu按钮
const showUpdateSpu = (row: SpuItem) => {
  emit("setIsShow", 2);
  spuStore.spu = row;
};
// 删除Spu按钮
const delSpuItem = async (row: SpuItem) => {
  await deleteSpuApi(row.id as number);
  getSpuList();
  ElMessage.success("删除成功~~");
};
// 监视三级id发生改变
watch(
  () => categoryStore.category3Id,
  () => {
    getSpuList();
  },
  { immediate: true }
);
</script>

<style scoped></style>
